package com.china08.yxyapi.api.manage.tec.teaching.continuedu.teatrainlesson;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.continuedu.YxyContinueduTask;
import com.china08.yxyapi.entity.mg.continuedu.YxyContinueduUserTask;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.continuedu.YxyContinueduTaskRepository;
import com.china08.yxyapi.repository.mg.continuedu.YxyContinueduUserTaskRepository;
import com.china08.yxyapi.util.DateToolUtils;

/**
 * 
 * @annotation: 继续教育-师训课程-通知任务查询
 * @author: yaoguanglin
 */
@CrossOrigin
@RestController
public class YxyTeaTrainLessonTaskSchController extends YxyApiBaseController {

    @Autowired
    private YxyContinueduTaskRepository yxyContinueduTaskRepository;

    @Autowired
    private YxyContinueduUserTaskRepository yxyContinueduUserTaskRepository;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/continuedu/teatrainlesson/task/sch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        // 获取学校所在省的ID和用户ID
        String areaIdProvince = getProfile(request, response).getSchoolAreaIdProvince();
        String userId = getProfile(request, response).getId();

        // 查询用户未完成或者已完成的任务
        List<YxyContinueduUserTask> tasks = yxyContinueduUserTaskRepository.findByUserIdAndStatusNot(userId, 0);

        // 查询用户进行中的任务
        List<YxyContinueduUserTask> tasksList = yxyContinueduUserTaskRepository.findByUserIdAndStatus(userId, 0);

        // 未完成或已完成的任务ID
        List<String> taskIds = new ArrayList<String>();

        // 进行中的任务ID
        List<String> taskIdList = new ArrayList<String>();

        for (YxyContinueduUserTask yxyContinueduUserTask : tasksList) {
            taskIdList.add(yxyContinueduUserTask.getTaskId());
        }

        List<YxyContinueduTask> taskList = new ArrayList<YxyContinueduTask>();

        // 查询当前学校所在省下用户未报名或者进行中的任务
        if (tasks != null && tasks.size() > 0) {
            // 取出任务ID
            for (YxyContinueduUserTask yxyContinueduUserTask : tasks) {
                taskIds.add(yxyContinueduUserTask.getTaskId());
            }

            // 查询未报名或者进行中的任务
            taskList = yxyContinueduTaskRepository.findByAreaCodeProvinceAndIdNotInAndStatus(areaIdProvince, taskIds,
                    0);
        } // 如果用户当前没有任务，查询当前学校所在省下所有任务
        else {
            taskList = yxyContinueduTaskRepository.findByAreaCodeProvinceAndStatus(areaIdProvince, 0);
        }
        List<RespModel> respModels = new ArrayList<RespModel>();

        if (taskList.size() > 0) {
            for (YxyContinueduTask yxyContinueduTask : taskList) {
                RespModel model = new RespModel();
                model.endDate = DateToolUtils.format(yxyContinueduTask.getEndDate(), DateToolUtils.FORMAT_SHORT_YMD);
                model.taskId = yxyContinueduTask.getId();
                model.taskName = yxyContinueduTask.getName();
                if (taskIdList.contains(yxyContinueduTask.getId())) {
                    model.status = 1;
                } else {
                    model.status = 0;
                }
                respModels.add(model);
            }
        }

        return respModels;
    }

    protected static class ReqModel extends YxyApiReqModel {
    }

    protected static class RespModel extends YxyApiRespModel {
        // 任务ID
        public String taskId;
        // 任务名称
        public String taskName;
        // 截止时间
        public String endDate;
        // 任务状态 0 未报名 1 进行中
        public Integer status;
    }
}
